from tkinter import *
from localc import Proposition


def show_result(proposition_str, output_var):
    p = Proposition(proposition_str)
    output_var.set(str(p.value))


def main_gui():
    root = Tk()
    root.title("Localc")
    from os import path
    path_to_icon = path.abspath(path.join(path.dirname(__file__), 'icon.ico'))
    root.iconbitmap(path_to_icon)
    input_row = Frame(root)
    input_row.pack(side=TOP, expand=YES, fill=X)
    proposition_label = Label(input_row, text="Proposition: ")
    proposition_label.pack(side=LEFT)

    calc_button = Button(input_row, text='Calculate')
    calc_button.pack(side=RIGHT)

    proposition_text = Entry(input_row, width=25)
    proposition_text.pack(side=RIGHT, fill=X, expand=YES)
    proposition_text.focus()

    result_row = Frame(root)
    result_row.pack(side=TOP, expand=YES, fill=X)

    value_label = Label(result_row, text="Value: ")
    value_label.pack(side=LEFT)

    var = StringVar()
    value = Label(result_row, textvariable=var, relief=SUNKEN)
    value.pack(side=RIGHT, expand=YES, fill=X)
    var.set('?')

    calc_button.config(command=(lambda: show_result(proposition_text.get(), var)))
    proposition_text.bind('<Return>', (lambda x: show_result(proposition_text.get(), var)))

    root.mainloop()

